<?php
//sleep(5);
     
    include_once('connection/connection.php');
 	
	if(isset($_REQUEST['new_handpump'])){
	$data = json_decode($_REQUEST['new_handpump'],true);
//	print_r($data);
	

		if($data['function_status']!="FUNCTIONAL"){
		$data['opration_status']="";
		$data['quality_status']="";
		$data['yield']="";
		$data['seasonal_flunctuation']="";
		$data['fluctuation_min']="";
		$data['fluctuation_max']="";
		$data['ph']="";
		$data['tds']="";
		$data['cl']="";
		$data['f']="";
		$data['no3']="";
		$data['date']="";
		}
		else{
		$data['Repairable']="";
		$data['remarks']="";
		}
		list($habit_code,$habit_name)=explode(",",$data['habitation_name']);
		if($data['hp_id']=="") {
		mysql_query("INSERT INTO tbl_dtls_hp_details_q1(loc_id,habit_code,habitation_name,type_of_habitation,hp_sp,location,latitude,longitude,	functional_status,operational_status,quality_status,yeild,sea_fluct_yeild,water_table_fluct_min,water_table_fluct_max,ph,tds,cl,f,no3,para_date,last_modified,repairable_status,remark,status) VALUES('$data[loc_id]','$habit_code','$habit_name' ,'$data[habit_type]','HP','$data[location]','$data[latitude]','$data[longitude]','$data[function_status]','$data[opration_status]','$data[quality_status]','$data[yield]','$data[seasonal_flunctuation]','$data[fluctuation_min]','$data[fluctuation_max]','$data[ph]','$data[tds]','$data[cl]','$data[f]','$data[no3]','$data[date]',NOW(),'$data[Repairable]','$data[remarks]','1')");
		$last_id = mysql_insert_id();
		$data['hp_id'] = $last_id;
		mysql_query("UPDATE tbl_dtls_q1_main SET hp_nos= hp_nos+1 WHERE id='$data[loc_id]' and hp_nos < (select count(hp_sp) from tbl_dtls_hp_details_q1 where loc_id='$data[loc_id]' and hp_sp='HP')");
		

		}
		
		
		else{
			mysql_query("UPDATE tbl_dtls_hp_details_q1 SET habit_code='$habit_code', habitation_name='$habit_name', type_of_habitation='$data[habit_type]', location='$data[location]', latitude='$data[latitude]' , longitude='$data[longitude]' , functional_status='$data[function_status]' , operational_status='$data[opration_status]' , quality_status='$data[quality_status]' , yeild='$data[yield]' , sea_fluct_yeild='$data[seasonal_flunctuation]' , water_table_fluct_min='$data[fluctuation_min]' , water_table_fluct_max='$data[fluctuation_max]' , ph='$data[ph]' , tds ='$data[tds]' , cl='$data[cl]',f='$data[f]',no3='$data[no3]' , para_date='$data[date]' , last_modified=NOW(),repairable_status='$data[Repairable]',remark='$data[remarks]'  WHERE id='$data[hp_id]'");
		}
		
		mysql_query("INSERT INTO tbl_dtls_hp_details_q1_temp(loc_id,habit_code,habitation_name,type_of_habitation,hp_sp,location,latitude,longitude,	functional_status,operational_status,quality_status,yeild,sea_fluct_yeild,water_table_fluct_min,water_table_fluct_max,ph,tds,cl,f,no3,para_date,last_modified,repairable_status,remark,status) VALUES('$data[loc_id]','$habit_code','$habit_name' ,'$data[habit_type]','HP','$data[location]','$data[latitude]','$data[longitude]','$data[function_status]','$data[opration_status]','$data[quality_status]','$data[yield]','$data[seasonal_flunctuation]','$data[fluctuation_min]','$data[fluctuation_max]','$data[ph]','$data[tds]','$data[cl]','$data[f]','$data[no3]','$data[date]',NOW(),'$data[Repairable]','$data[remarks]','1')");
		
		$data['habitation_name']=$habit_name;
	echo json_encode($data);

	}
	
	if(isset($_REQUEST['edit_hp'])){
	$select_query = mysql_query("SELECT * FROM tbl_dtls_hp_details_q1 WHERE id='$_REQUEST[edit_hp]'");
	echo json_encode(mysql_fetch_assoc($select_query));
	}
	if(isset($_REQUEST['del_id'])){
	mysql_query("DELETE FROM tbl_dtls_hp_details_q1 WHERE id='$_REQUEST[del_id]'");
	mysql_query("UPDATE tbl_dtls_q1_main SET hp_nos= hp_nos-1 WHERE id='$_REQUEST[loc_id]'");
	echo $_REQUEST['del_id'];
	}

	 
  //echo mysql_error();
  ?>